import time

from curl_cffi import requests
import re
import os
import shutil
from util.headers import get_headers_init_user_agent

root = 'static/彼岸图库/'
if os.path.exists(root):
    shutil.rmtree(root)
os.makedirs(root)

page = 2
url = f'https://pic.netbian.com/index_{page}.html'
list_response = requests.get(url, headers=get_headers_init_user_agent())
html_content = list_response.content.decode('gbk')
items = re.findall(r'<li><a href="/tupian/(.*?).html" target="_blank"><img src="/uploads/allimg/(.*?).jpg" alt="(.*?)" /><b>.*?</b></a></li>', html_content, re.S)
for item in items:
    title = item[2]
    thumb_url = f'https://pic.netbian.com/uploads/allimg/{item[1]}.jpg'
    # print(items[1])
    headers = get_headers_init_user_agent()
    headers.update({
        'referer': url
    })
    thumb_response = requests.get(thumb_url, headers=headers)
    with open(f"{root}/{title}_thumb.jpg", "wb") as f:
        f.write(thumb_response.content)
    detail_url = f'https://pic.netbian.com/tupian/{item[0]}.html'
    detail_response = requests.get(detail_url, headers=get_headers_init_user_agent())
    detail_html = detail_response.content.decode('gbk')
    preview_url = re.search(r'<a href="" id="img"><img src="(.*?)" data-pic=".*?</a>', detail_html, re.S).group(1)
    # print(preview_url)
    preview_url = f'https://pic.netbian.com{preview_url}'
    headers = get_headers_init_user_agent()
    headers.update({
        'referer': detail_url
    })
    preview_response = requests.get(preview_url, headers=headers)
    with open(f"{root}/{title}_preview.jpg", "wb") as f:
        f.write(preview_response.content)
    time.sleep(1)


